<%@page import="org.springframework.security.core.context.SecurityContextHolder"%>
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@page import="org.apache.commons.lang.time.DateFormatUtils"%>
<%@page import="org.springframework.util.CollectionUtils"%>
<%@page import="java.util.Collection"%>
<%@ include file="../common/common_include.jsp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	AreaForm form = (AreaForm)request.getAttribute("command");
	TblOaAreaCoordinate coordinate = null;
	String x = "";
	String y = "";
	if(null!=request.getAttribute("tblOaAreaCoordinate")){
		coordinate = (TblOaAreaCoordinate)request.getAttribute("tblOaAreaCoordinate");
		x = coordinate.getLongitude();
		y = coordinate.getLatitude();
	}
%>

<style>
	#coordinate-popup{
		max-width:850px;
		max-height:700px;
		margin: 0 auto;
		position: relative;
		z-index:100px;
		background: #fff;
	}
</style>


<div id="coordinate-popup">
	<form id="AreaEditCoordinateForm" target="_coreIframe" method="post" action="<%=request.getContextPath()%>/area/doEditCoordinate">
		<input type="hidden" name="areaId" value="<%=form.getAreaId()%>">
		<table class="formTable">
			<tr>
				<td class="tdTitle">区域名称：</td>
				<td>
					<input class="inputText inputText-gray xxxs" type="text"  value="<%=form.getAreaName()%>" readonly="readonly">&nbsp;&nbsp;<%=StringUtils.isNotBlank(coordinate.getAddress())?coordinate.getAddress():"" %>
				</td>
			</tr>
			<tr>
				<td class="tdTitle">选择坐标：</td>
				<td class="" colspan="2">
					<div style="margin:10px 0;">
						<input type="text" name="addressText" id="addressText" 
						value="" class="inputText s">
						<a class="a-btn-microsoft" href="javascript:XF.localCoordinate()">定位</a>
					</div>
					<div id="ditu" style="width:700px;height:450px;">
						正在加载地图......
					</div>
				</td>
			</tr>
			
			<tr>
				<td class="tdTitle">已选择：</td>
				<td>
					<input class="inputText inputText-gray xs" type="text"  value=""  name="coordinate" readonly="readonly">
				</td>
			</tr>

			<tr>
				<td class="tdTitle"></td>
				<td>
					<div>
						<input class="input-btn-microsoft" type="submit" value="提交">
						<a class="a-btn-microsoft" href="javascript:XF.Area.closePopup();">取消</a>
					</div>
				</td>
			</tr>	
		</table>
	</form>
	<iframe id="_coreIframe" name="_coreIframe" style="display: none;"></iframe>
	<script type="text/javascript">
		window.onload = XF.loadMap();
	 	function initBaiduMap(){
			setTimeout(function(){
			if(typeof(BMap)!='undefined'){
				var map = new BMap.Map("ditu");            // 创建Map实例
				window.BaiduMap = map;
				var point = new BMap.Point(113.673, 22.815); // 创建点坐标
				map.centerAndZoom(point,11);                 
				map.enableScrollWheelZoom();     //启用滚轮放大缩小
				
				_areaCoordinateEdit(map);
				_areaAddressTextTip(map);
			}else{
				setTimeout(function(){
					initBaiduMap();
				},500);
			}
			},500);
		} 
		
		function _areaCoordinateEdit(map){
			map.setDefaultCursor("crosshair");
			<%
				if(StringUtils.isNotBlank(x) && StringUtils.isNotBlank(y)){
					%>
						var x = <%=x%>;
						var y = <%=y%>;
						var point = new BMap.Point(x,y); // 创建点坐标
						map.centerAndZoom(point,18); 
						var marker = new BMap.Marker(new BMap.Point(x, y));
						map.addOverlay(marker);  
						$("input[name=coordinate]").val(x+","+y);
					<%
				}
			%>
			map.addEventListener("click",function(e){
				map.clearOverlays();
				var x = e.point.lng;
				var y = e.point.lat;
				$("input[name=coordinate]").val(x+","+y);
				var marker = new BMap.Marker(new BMap.Point(x, y));
				map.addOverlay(marker);  
			});
		}
		
		function _areaAddressTextTip(map){
			var ac = new BMap.Autocomplete({
				"input" : "addressText",
				"location" : map
			});
			ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
				var str = "";
				var _value = e.fromitem.value;
				var value = "";
				if (e.fromitem.index > -1) {
					value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
				}    
				str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
				
				value = "";
				if (e.toitem.index > -1) {
					_value = e.toitem.value;
					value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
				}    
				str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
				$("#searchResultPanel").html(str);
				
			});
			
			var myValue;
			ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
			var _value = e.item.value;
				myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
				$("#searchResultPanel").html("onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue);
				setPlace();
			});
	
			function setPlace(){
				map.clearOverlays();    //清除地图上所有覆盖物
				function myFun(){
					var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
					map.centerAndZoom(pp, 18);
					map.addOverlay(new BMap.Marker(pp));    //添加标注
					$("input[name=coordinate]").val(pp.lng+","+pp.lat);
				}
				var local = new BMap.LocalSearch(map, { //智能搜索
				  onSearchComplete: myFun
				});
				local.search(myValue);
			}
		}
	</script>
</div>


























